/*
*
*
*/
(function($) {
    var settings = {
        containerSelector:"",
        navClass:"",
        navParentADom:function(data){
            return 
        },
        navChildrenADom:function(data){
            return 
        },
        nodeNavClass:"",
        nodeParentADom:function(data){
            return 
        },
        nodeChildrenADom:function(data){
            return 
        },
        breadcrumbSelector:"",
        breadcrumbClass:"",
        breadcrumbDom: "",
        parentActiveClass:"",
        childrenActiveClass:"",
        positionNode:"",
        level:0,
        data:[],
        clickParentCallBack:function(e){},
        clickChildrenCallBack:function(e){},
        positionNodeCallBack:function(e){},
        refreshCallBack:function(){}
    };
    $.nav = {
        initNav:function(option){
            $.extend(settings, option);
            $.nav.refreshNav();
        },
        refreshNav: function() {            
            var containerSelector = settings.containerSelector,
            navClass = settings.navClass,
            positionNode = settings.positionNode;
            level = settings.level,
            data = settings.data;
            containerSelector.html("");
            var $nav = $("<ul class='"+navClass+"'></ul>");
            var array = []
            for (var i in data) {
                if (typeof(data[i].children)=="undefined" || data[i].children.length > 0) {
                    var children = data[i].children;
                    var $nav1 = $("<li class=''></li>");
                    var nameArray = [];
                    var $a = settings.navParentADom(data[i]);
                    $a.attr("href",array.join("")+"#"+data[i].name);
                    $a.attr("type","parent");
                    $a.attr("level",level);
                    $a.attr("data",JSON.stringify(data[i]));
                    $a.attr("name",data[i].name);

                    nameArray = array.concat(nameArray);
                    nameArray.push("#"+data[i].name);

                    $nav1.append($a);
                    $.nav.addNode($nav1, children,level+1,nameArray);
                    $nav.append($nav1);
                } else {
                    var $nav1 = $("<li class=''></li>")
                    var $a = settings.navChildrenADom(data[i]);

                    $a.attr("href",array.join("")+"#"+data[i].name);
                    $a.attr("type","children");
                    $a.attr("level",level);
                    $a.attr("data",JSON.stringify(data[i]));  
                    $a.attr("name",data[i].name);

                    $nav1.append($a);
                    $nav.append($nav1);
                }                
            }
            containerSelector.append($nav);
            //如果当前传入的面包屑容器存在则初始化面包屑
            if(settings.breadcrumbSelector.html()){
                $.nav.initBreadcrumb();
            }
            $.nav.clickParent();
            $.nav.clickChildren();
            $.nav.positionNode(positionNode);
            settings.refreshCallBack();
            // console.log(settings.breadcrumbSelector.attr("class"))
            
        },
        addNode: function(sidebar,data,level,array) {
            // console.log(array)
            var nodeNavClass = settings.nodeNavClass;
            var $nav = $("<ul class='"+nodeNavClass+"'></ul>");
            for (var i in data) {
                var name = data[i].name;
                var type = data[i].type;
                if (typeof(data[i].children)!="undefined" && data[i].children.length > 0) {
                    var children = data[i].children;
                    var $nav1 = $("<li class=''></li>");
                    var nameArray = [];
                    var $a = settings.nodeParentADom(data[i]);
                    $a.attr("href",array.join("")+"#"+data[i].name);
                    $a.attr("type","parent");
                    $a.attr("level",level);
                    $a.attr("data",JSON.stringify(data[i]));
                    $a.attr("name",data[i].name);

                    nameArray = array.concat(nameArray);
                    nameArray.push("#"+data[i].name);

                    $nav1.append($a);
                    $.nav.addNode($nav1, children,level+1,nameArray);
                    $nav.append($nav1);
                } else {
                    var $nav1 = $("<li></li>");

                    var $a = settings.nodeChildrenADom(data[i]);
                    $a.attr("href",array.join("")+"#"+data[i].name);
                    $a.attr("type","children");
                    $a.attr("level",level);
                    $a.attr("data",JSON.stringify(data[i]));
                    $a.attr("name",data[i].name);

                    $nav1.append($a);
                    $nav.append($nav1);
                }
            }
            sidebar.append($nav)
        },
        clickParent: function(){
            $("a[type='parent']").unbind("click");
            $("a[type='parent']").bind("click",function(){
                $(this).next("ul").toggle();
                $(this).parent().siblings().find("ul").hide();
                $(this).parent().addClass(settings.parentActiveClass);
                $(this).parent().siblings().removeClass(settings.parentActiveClass).find(settings.childrenActiveClass).removeClass(settings.parentActiveClass);
                settings.clickParentCallBack($(this));
                // console.log($(this).attr("href"))
                $.nav.addBreadcrumb($(this).attr("href"),$(this).attr("name"),$(this).attr("level"));
            });
        },
        clickChildren: function(){
            $("a[type='children']").unbind("click");
            $("a[type='children']").bind("click",function(){
                $(this).addClass(settings.childrenActiveClass);
                $(this).parent().siblings().find("ul").hide();
                $(this).parent().addClass(settings.childrenActiveClass);
                $(this).parent().siblings().removeClass(settings.parentActiveClass).find("."+settings.childrenActiveClass).removeClass(settings.childrenActiveClass);
                settings.clickChildrenCallBack($(this));
                // console.log($(this).attr("href"))
                $.nav.addBreadcrumb($(this).attr("href"),$(this).attr("name"),$(this).attr("level"));
            });
        },
        positionNode: function(name) {
            $("."+settings.parentActiveClass).removeClass(settings.parentActiveClass);
            $("."+settings.childrenActiveClass).removeClass(settings.childrenActiveClass);
            var nameArray = name.split("#");
            if(nameArray.length<1){return}
            var parentObj = settings.containerSelector;
            var e;
            for(var i =1;i<nameArray.length;i++){
                e = $("a[name='"+nameArray[i]+"']",parentObj);
                parentObj=e.parent();
                e.trigger("click")
            }
            settings.positionNodeCallBack(e);
        },
        initBreadcrumb: function(){
            settings.breadcrumbSelector.html("");
            var $ul = $("<ul class='"+settings.breadcrumbClass+"'></ul>");
            for(var i=0;i <= 20;i++){           
                var $li = $(settings.breadcrumbDom);
                $li.append("<a class='text'>"+i+"</a>");
                $ul.append($li);
                $li.hide();
            }
            settings.breadcrumbSelector.append($ul);
            $.nav.ClickBreadcrumb();
            // console.log($("."+settings.breadcrumbClass).find("li").eq(0).find(".text").html())
        },
        addBreadcrumb:function(href,name,level){
            // settings.breadcrumbDom.append();
            // $("."+settings.breadcrumbClass).append(settings.breadcrumbDom);
            level = parseInt(level);
            // console.log(href);
            // console.log(name);
            // console.log(level);
            $("."+settings.breadcrumbClass).find("li").show();
            $("."+settings.breadcrumbClass).find("li").eq(level).find(".text").attr("href",href).html(name);
            $("."+settings.breadcrumbClass).find("li").eq(level).nextAll().hide();
        },
        ClickBreadcrumb:function(){
            $(".text").bind("click",function(){
                settings.positionNode = $(this).attr("href");
                $.nav.refreshNav();
                //console.log($(this).attr("href"))
            });
        }
    };

})(jQuery);